<template>
  <vab-dialog :title="dialogTitle" v-model="isShowDialog" :close-on-click-modal="true" show-fullscreen width="80%">
    <template #default>
      <CommonForm
        ref="commonFormRef"
        v-model="form"
        v-loading="formLoading"
        :layout-config="layoutConfig"
        :option="option"
        @on-cancel="onHandleCancel"
        @on-success="onHandleSuccess"
      />
    </template>
  </vab-dialog>
</template>

<script>
import CommonForm from "@/plugins/form/CommonForm.vue";
import VabDialog from "@/plugins/VabDialog/index.vue";
import { TableColumnOrFormTypeEnum } from "@/plugins/commonType.enum";
export default defineComponent({
  name: "JobEdit",
  components: {
    CommonForm,
    VabDialog
  },
  emits: { "on-success": null },
  setup(props, { expose, emit }) {
    const state = reactive({
      dialogTitle: "新增",
      detailUrl: "monitor/job",
      addUrl: "monitor/job/addJob",
      updateUrl: "monitor/job/updateJob",
      isShowDialog: false,
      formLoading: false,
      layoutConfig: {
        //form组件布局
        column: 2
      }
    });

    const form = ref({});
    const option = {
      column: [
        {
          label: "任务名称",
          prop: "jobName",
          type: TableColumnOrFormTypeEnum.INPUT
        },
        {
          label: "任务分组",
          prop: "jobGroup",
          type: TableColumnOrFormTypeEnum.SELECT,
          placeholder: "请选择任务分组",
          dictCode: "sys_job_group"
        },
        {
          label: "调用方法",
          prop: "invokeTarget",
          type: TableColumnOrFormTypeEnum.INPUT
        },
        {
          label: "cron表达式",
          prop: "cronExpression",
          type: TableColumnOrFormTypeEnum.INPUT
        },
        {
          label: "执行策略",
          prop: "misfirePolicy",
          type: TableColumnOrFormTypeEnum.SELECT,
          dictCode: "jobMisfirePolicy"
        },
        {
          label: "是否并发",
          prop: "concurrent",
          type: TableColumnOrFormTypeEnum.SELECT,
          dictCode: "WhetherEnum"
        },
        {
          label: "状态",
          prop: "status",
          type: TableColumnOrFormTypeEnum.SELECT,
          dictCode: "sys_job_status"
        }
      ],
      formConfig: {
        title: "",
        isInModal: true
      },
      rules: {
        jobName: [
          {
            required: true,
            message: "请输入任务名称",
            trigger: "blur"
          },
          {
            min: 2,
            max: 20,
            message: "任务名称长度为2到100",
            trigger: "blur"
          }
        ],
        invokeTarget: [
          {
            required: true,
            message: "请输入调用方法",
            trigger: "blur"
          },
          {
            min: 2,
            max: 100,
            message: "调用方法为2到100",
            trigger: "blur"
          }
        ],
        cronExpression: [
          {
            required: true,
            message: "请输入cron表达式",
            trigger: "blur"
          },
          {
            min: 2,
            max: 100,
            message: "cron表达式为2到100",
            trigger: "blur"
          }
        ],
        jobGroup: [
          {
            required: true,
            message: "请输入选择任务分组",
            trigger: "blur"
          }
        ]
      },
      submitUrl: state.addUrl
    };

    const showDialog = row => {
      state.formLoading = true;
      if (row && row.id) {
        state.dialogTitle = "编辑";
        form.value = row;
        option.submitUrl = state.updateUrl;
      } else {
        state.dialogTitle = "新增";
        form.value = {};
        option.submitUrl = state.addUrl;
      }
      state.formLoading = false;
      state.isShowDialog = true;
    };

    const onHandleSuccess = () => {
      emit("on-success");
      state.isShowDialog = false;
    };

    const onHandleCancel = () => {
      state.isShowDialog = false;
    };

    expose({ showDialog });
    return {
      ...toRefs(state),
      form,
      option,
      onHandleSuccess,
      onHandleCancel
    };
  }
});
</script>
